package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.commons.huanxin.utils.HuanXinUtils;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.Contacts;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.server.api.UserInfoApi;
import com.tanhua.dubbo.server.api.mongo.FriendApi;
import com.tanhua.server.interceptor.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class IMService {

    @Reference
    private FriendApi friendApi;

    @Reference
    private UserInfoApi infoApi;

    @Autowired
    private HuanXinUtils huanXinUtils;

    public ResponseEntity contactUser(Long friendId) {
        // 1、保存数据到MongoDB
        Friend friend = new Friend();
        friend.setUserId(UserThreadLocal.getUserId());
        friend.setFriendId(friendId);
        friendApi.save(friend);

        // 2、好友关系注册到环信
        huanXinUtils.contactUsers(UserThreadLocal.getUserId(), friendId);

        return ResponseEntity.ok(null);
    }


    public ResponseEntity queryContactsList(Integer page, Integer pageSize, String keyword) {

        PageResult result = friendApi.findAll(page, pageSize, UserThreadLocal.getUserId(), keyword);

        List<Friend> items = (List<Friend>) result.getItems();
        List<Contacts> list = new ArrayList<>();
        for (Friend friend : items) {

            UserInfo userInfo = infoApi.findByUserId(friend.getFriendId());
            Contacts contacts = new Contacts();
            BeanUtils.copyProperties(userInfo, contacts);
            contacts.setCity(StringUtils.substringBefore(userInfo.getCity(),"-"));
            contacts.setUserId(userInfo.getId().toString());
            list.add(contacts);
        }
        result.setItems(list);

        return ResponseEntity.ok(result);
    }


//
//
//    public PageResult queryMessageLikeList(Integer page, Integer pageSize) {
//        return this.messageCommentList(1, page, pageSize);
//    }
//
//    public PageResult queryMessageCommentList(Integer page, Integer pageSize) {
//        return this.messageCommentList(2, page, pageSize);
//    }
//
//    public PageResult queryMessageLoveList(Integer page, Integer pageSize) {
//        return this.messageCommentList(3, page, pageSize);
//    }
//
//    private PageResult messageCommentList(Integer type, Integer page, Integer pageSize) {
//        User user = UserThreadLocal.get();
//
//        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
//
//        Query query = new Query(Criteria
//                .where("publishUserId").is(UserThreadLocal.getUserId())
//                .and("commentType").is(type)).with(pageRequest);
//
//        List<Comment> commentList = this.mongoTemplate.find(query, Comment.class);
//
//        List<MessageLike> messageLikeList = new ArrayList<>();
//
//        for (Comment record : commentList) {
//            MessageLike messageLike = new MessageLike();
//            messageLike.setId(record.getId().toHexString());
//            UserInfo info = userInfoService.findById(record.getUserId());
//            messageLike.setAvatar(info.getAvatar());
//            messageLike.setNickname(info.getNickname());
//            messageLike.setCreateDate(new DateTime(record.getCreated()).toString("yyyy-MM-dd HH:mm"));
//            messageLikeList.add(messageLike);
//        }
//
//        PageResult pageResult = new PageResult(page,pageSize);
//        pageResult.setItems(messageLikeList);
//        return pageResult;
//    }
//
//
//



}
